package com.ucampus.controller;

import com.ucampus.entity.Building;
import com.ucampus.entity.ResponseData;
import com.ucampus.service.BuildingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/building")
public class BuildingController {
    @Autowired
    private BuildingService buildingService;

    @GetMapping("/")
    public ResponseData getAll() {
        return buildingService.getAll();
    }

    @GetMapping("/select")
    public ResponseData getByName(@RequestParam("name") String name) {
        return buildingService.getByName(name);
    }

    @GetMapping("/{id}")
    public ResponseEntity<byte[]> getImage(@PathVariable int id) {
        byte[] image = buildingService.getImage(id);
        return ResponseEntity.ok().contentType(MediaType.IMAGE_JPEG).body(image);
    }

    @PostMapping("/upload")
    public ResponseData add(@RequestBody Building building) throws IOException {
        return buildingService.add(building.getName(),building.getIntroduction());
    }

    @PutMapping("/update")
    public ResponseData update(@RequestBody Building building) throws IOException {
        return buildingService.update(building.getId(),building.getName(),building.getIntroduction());
    }

    @DeleteMapping("/delete")
    public ResponseData delete(@RequestBody List<Integer> list) {
        return buildingService.delete(list);
    }
}
